package com.koron.order.workbench.homeCard.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.business.datadict.bean.po.SysDataDict;
import com.koron.common.core.business.datadict.bean.po.SysDataDictDetail;
import com.koron.common.core.business.datadict.service.SysDataDictService;
import com.koron.common.core.business.datadict.utils.DataDictTool;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.order.workbench.homeCard.bean.dto.HomeCardDto;
import com.koron.order.workbench.homeCard.bean.po.HomeCardPo;
import com.koron.order.workbench.homeCard.mapper.HomeCardMapper;
import com.koron.order.workbench.homeCard.service.HomeCardService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.ADOConnection;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 小组件卡片表(HomeCard)表服务实现类
 *
 * @author zhouj
 * @since 2022-03-18 10:19:36
 */
@Service("homeCardService")
public class HomeCardServiceImpl implements HomeCardService {

    @Autowired
    private SysDataDictService dictService;

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, HomeCardPo homeCard) {

        HomeCardMapper mapper = factory.getMapper(HomeCardMapper.class);

        return mapper.insertOrUpdate(homeCard);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<HomeCardPo> homeCardList) {

        HomeCardMapper mapper = factory.getMapper(HomeCardMapper.class);

        return mapper.insertOrUpdateBatch(homeCardList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, HomeCardPo homeCard) {

        HomeCardMapper mapper = factory.getMapper(HomeCardMapper.class);

        return mapper.insert(homeCard);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, HomeCardDto homeCardDto) {

        HomeCardMapper mapper = factory.getMapper(HomeCardMapper.class);
        String appId = homeCardDto.getAppId();
        String account = homeCardDto.getAccount();

        if (StringUtils.isEmpty(appId)) {
            appId = ThreadLocalContext.getApp().getId();
        }

        if (StringUtils.isEmpty(account)) {
            account = ThreadLocalContext.get().getAccount();
        }

        Assert.isTrue(StringUtils.isNotEmpty(appId));
        Assert.isTrue(StringUtils.isNotEmpty(account));

        //先删除该水司下账号-卡片信息
        mapper.deleteByAppId(appId, account);

        List<HomeCardPo> homeCardPos = homeCardDto.getHomeCardPos();

        if (CollectionUtils.isEmpty(homeCardPos)) {
            return 0;
        }
        for (int i = 0; i < homeCardPos.size(); i++) {
            homeCardPos.get(i).setSort(i);
            homeCardPos.get(i).setAppId(appId);
            homeCardPos.get(i).setAccount(account);
        }

        return mapper.insertBatch(homeCardPos);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, HomeCardPo homeCard) {
        HomeCardMapper mapper = factory.getMapper(HomeCardMapper.class);
        return mapper.update(homeCard);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        HomeCardMapper mapper = factory.getMapper(HomeCardMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    public int deleteByAppId(SessionFactory factory, String appId, String account) {
        HomeCardMapper mapper = factory.getMapper(HomeCardMapper.class);
        return mapper.deleteByAppId(appId, account);
    }

    @Override
    @TaskAnnotation("queryById")
    public HomeCardPo queryById(SessionFactory factory, String id) {

        HomeCardMapper mapper = factory.getMapper(HomeCardMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<HomeCardPo> queryList(SessionFactory factory, HomeCardPo homeCard) {

        HomeCardMapper mapper = factory.getMapper(HomeCardMapper.class);

        String appId = ThreadLocalContext.getApp().getId();
        String account = ThreadLocalContext.get().getAccount();

        if (StringUtils.isEmpty(homeCard.getAccount())) {
            homeCard.setAccount(account);
        }

        if (StringUtils.isEmpty(homeCard.getAppId())) {
            homeCard.setAppId(appId);
        }

        //查询个人的首页卡片小组件列表
        List<HomeCardPo> list = mapper.queryList(homeCard);

        Map<String, String> dataDictByCode = DataDictTool.getDataDictByCode("publicDictionary", "home_card");

        List<HomeCardPo> homeCardPoList = getHomeCardPoList(appId, account);

        if (CollectionUtils.isEmpty(homeCardPoList)) {
            return homeCardPoList;
        }

        //赋值个人的首页小组件个人配置
        if (CollectionUtils.isNotEmpty(list)) {

            Map<String, HomeCardPo> map = list.stream().collect(Collectors.toMap(HomeCardPo::getCardCode, t -> t));

            homeCardPoList.forEach(p -> {

                HomeCardPo homeCardPo = map.get(p.getCardCode());

                if (!Objects.isNull(homeCardPo)) {
                    p.setDisplay(homeCardPo.getDisplay());
                    p.setSort(homeCardPo.getSort());
                }

            });

            //排序
            homeCardPoList.sort(new Comparator<HomeCardPo>() {
                @Override
                public int compare(HomeCardPo p1, HomeCardPo p2) {
                    return p1.getSort() - p2.getSort();
                }
            });

        }

        return homeCardPoList;
    }

    //获取首页小卡片列表
    public List<HomeCardPo> getHomeCardPoList(String appId, String account) {
        List<HomeCardPo> homeCardPos = new ArrayList<>();
        SysDataDict sysDataDict = ADOConnection.runTask(dictService, "getByClassifyIdAndCode", SysDataDict.class, "publicDictionary", "home_card");
        List<SysDataDictDetail> sysDataDictDetailList = sysDataDict.getSysDataDictDetailList();

        for (SysDataDictDetail sysDataDictDetail : sysDataDictDetailList) {
            HomeCardPo homeCardPo = new HomeCardPo();
            homeCardPo.setAppId(appId);
            homeCardPo.setAccount(account);
            homeCardPo.setCardCode(sysDataDictDetail.getKey());
            homeCardPo.setCardName(sysDataDictDetail.getValue());
            homeCardPo.setDisplay(true);
            homeCardPo.setSort(sysDataDictDetail.getSort());
            homeCardPos.add(homeCardPo);
        }
        return homeCardPos;
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, HomeCardPo homeCard, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        HomeCardMapper mapper = factory.getMapper(HomeCardMapper.class);

        List<HomeCardPo> list = mapper.queryList(homeCard);

        return new PageInfo(list);
    }

}
